<?php 
/**
 * 产品信息管理
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	账目信息
 * @package  	Action
 * @author    	李恺弘
 * @version 	2.1,2012-07-22
 */

class ProductPublicAction extends RelationCommonAction {
 
	protected $_default_post	=  array('query'=>array('to_hide'=>1));  ///默认post值处理
    public $_view_dir 			= 'Basic';	/// 定义模板显示路径
    public $_sortBy				= 'product_no';
    protected $_cacheDd 		= array(8); 
	///自动编号  
	public $_setauto_cache		= 'setauto_product_no';	///编号对应超管配置中的值    
	public $_auto_no_name		= 'product_no';		 	///编号no 
	
	 public $_show_field			= array('new_class' 	=> array(1, 2, 3),
    									'factory_name' 	=> array(1, 2, 4),
    									'new_factory' 	=> array(1, 2),
    									'new_color'		=> array(1, 2, 4),
    									'new_size'		=> array(1, 2, 4),
    									'instock_price' => array(1, 2),
    									'wholesale_price' => array(1, 3),
    									'retail_price' => array(1, 3, 5),
    									'sale_price' => array(1, 3),
    									);
	
	 public function __construct() { 
    	parent::__construct(); 
    	foreach ($this->_show_field as $key => $v) {
    		$show_result[$key] = 0;
    		if (in_array(getUser('role_type'), $v) || $this->admin) {
    			$show_result[$key] = 1;
    		}
    	}     	
    	$this->assign("show_price_title", getUser('role_type') == 4 ? 0 : 1);
    	$this->assign("show_field", $show_result);
    	if (getUser('role_type')==4 && C('product_factory')==1) {
			$_POST['query'] = array('factory_id'=>intval(getUser('company_id')));
		}
    }
 
	///新增
	public function add() {
	 	///产品自动编号
		$this->_autoMaxNo();	
		/// 厂家用户只能看到自己的	
		$userInfo	=	getUser(); 
		if ($userInfo['role_type'] ==4 && C("PRODUCT_FACTORY")) {  
			$userInfo['company_id']   =1;
    		$this->assign("fac_id", $userInfo['company_id']);
    		$cache = SOnly('factory',$userInfo['company_id']); 
    		$this->assign("fac_name", $cache['factory_name']);
    	}   
		$this->assign('sid',session_id());
		$this->assign('file_tocken',md5(time()));
		$userInfo	= getUser();
		$role_type	= $userInfo['role_type'];
		$role_type_where = ($role_type ==1||$_SESSION[C('ADMIN_AUTH_KEY')]==true) ? " " : " and role_type in (1,".$role_type.")";
		$properties = M('Properties')->where('to_hide=1'.$role_type_where)->select();/// 取产品属性信息	 
		$this->assign('properties',$properties);
		$this->assign('class_level',C('PRODUCT_CLASS_LEVEL'));
		$this->assignCurrency();
	 
	} 
	 
	
	/// 生成单价币种说明，中文
    private function assignCurrency(){
    	$temp 	= S('currency');
    	$cur	= array('in'=>$temp[1]['currency_no'],'out'=>$temp[C('CURRENCY')]['currency_no']);
    	$this->assign('cur',$cur);
    }
    
    /// 新增
	public function insert() {   
		///获取当前Action名称
		$name = $this->getActionName();
 		///获取当前模型
		$model 	= D($name);  
		///重新组合POST来的信息 
		$info	= $model->setPost($_POST);   
		///模型验证
		if ($model->create($info)) {	 
			$id = $model->relation(true)->add(); 
			if (false === $id) {
				$this->error (L('_ADD_FAILED'));
			}
			$this->checkCacheDd($id); 
			$this->id	=	$id;
		} else {
			$this->error ( $model->getError(),2);
		}
///		$this->success('OK'); 
	}

	public function _after_insert(){ 
		$info['id']	=	$this->id; 
		if(!empty($_POST['file_tocken'])){
			D('Gallery')->update($this->id,$_POST['file_tocken']);//更新产品图片关联信息
		}
		tag('product',$info); // 添加action_init 标签   
		$this->success('操作成功'); 
	}
	   
    /// 修改产品
    public function edit() {
    	///产品自动编号
		$this->_autoMaxNo();	
    	$this->id = $id = intval($_GET['id']);
		if ($id>0) {
			///获取当前Action名称
			$name = $this->getActionName();
	 		///获取当前模型
			$model 	= D($name); 
			
///			$this->addJs('uploadify/swfobject.js');
///			$this->addJs('uploadify/uploadify.js');
			$this->assign('sid',session_id());
			$this->assign('file_tocken',md5(time()));
			$this->assign('class_level',C('PRODUCT_CLASS_LEVEL')); 
			$vo = $model->getInfo($id);
			$model->cacheLockVersion($vo); 
			$rate = $model->getRate(); 
			$role = $model->getRole();
			$this->assignCurrency();   
			$this->assign ('rs',$vo); 
			$this->assign ('rate',$rate);
			$this->assign ('role',$role);
		}else {
			$this->error(L('_ERROR_'));
		}
		$this->display ();
    }
    
    public function _after_edit(){ 
		$info['id']	=	$this->id;
		tag('product',$info); /// 添加action_init 标签  
	}
    
    
    /// 查看产品明细
    public function view(){
    	$id = intval($_GET['id']);
		if ($id>0) {
			///获取当前Action名称
			$name = $this->getActionName();
	 		///获取当前模型
			$model 	= D($name);  
			$vo 	= $model->getInfo($id,'view');
			$role = $model->getRole();
			$this->assign ('rs',$vo);
			$this->assign ('role',$role);
			$this->assign ('dd_color',S('color'));
			$this->assign ('dd_size',S('size'));
			$this->assignCurrency();
		}else {
			$this->error(L('_ERROR_')); 
		}  
    }
    
    public function update() {	
		///获取当前Action名称
		$name = $this->getActionName();
 		///获取当前模型
		$model 	= D($name);  
		$info	=	$model->setPost($info);   
		if ($model->create($info)) {			
			$r = $model->relation(true)->save(); 	
			if (false === $r) {
				$this->error (L('EDIT_FAILED'));
			} 
			$this->checkCacheDd($info['id']);
			$this->id	=	$info['id'];  
			tag('product',$info); /// 添加action_init 标签  
		} else {
			$this->error ( $model->getError() ,$model->errorStatus);
		}
	}
    
     
    public function updateProductClassInfo(){
    	$a = new Model();
    	$a->execute('delete from product_class_info');
    	$list = M('Product')->findAll();
    	foreach ($list as $value) {
    		$data = M('Product')->getProductClassInfo($value['product_class_id']);
    		$data['product_id'] = $value['id'];
    		M('ProductClassInfo')->add($data);
    	}
    	exit('OK');
    }
    
    public function index(){
    	/// 设置查询条件
    	if (isset($_POST['pics'])) {
    		if($_POST['pics']==1){
    			$_POST['gallery']['query']['id'] = 'is not null';
	    		$where	= 'c.id is not null';
	    	}elseif ($_POST['pics']==0){
	    		$_POST['gallery']['query']['id'] = 'is null';
	    	}
    	}
    	if (ROLE_TYPE ==4 && C("PRODUCT_FACTORY")) {    	
    		$_POST['main']['query']['factory_id'] = intval(CLI_FAC_ID);
    	}
    	///获取当前Action名称
		$name = $this->getActionName();
 		///获取当前模型
		$model 	= D($name);   
		$this->list = $model->getIndexList();
		getOutPutRand();
		if ($_POST['search_form']) {
			$this->display ('list');
		}else {
			$this->display ();
		}
    }
    
    public function delete() {
    	///还原特殊处理 mingxing 
    	if ($_GET['restore']==1){
    		$this->restore();
    	}else{ 
			///获取当前Action名称
			$name = $this->getActionName();
	 		///获取当前模型
			$model 	= D($name);  
			$pk		= $model->getPk (); 
	    	$id 	= intval($_REQUEST[$pk]); 
	    	$condition	=	'id='.$id;
	    	$this->id = $id;
	    	$list	=	$model->where($condition)->setField('to_hide',2);   
			if (false === $list) {
				$this->error ( L('DELETE_FAILED') );
			}
		}
	}
	
	///还原
    public function restore($id=null){
    	///获取当前Action名称
	 	$name = $this->getActionName();
 		///获取当前模型
		$model 	= D($name);   
		///当前主键
		$pk		=	$model->getPk();
		$id 	= 	$id ? intval($id) : intval($_REQUEST[$pk]);
		$this->id = $id;
		if ($id>0) { 
			///更新条件
			$condition 	= $pk.'='.$id; 
			///执行条件语句
			$list		= $model->where( $condition )->setField('to_hide',1);    
			///如果产品还原失败提示失败
			if ($list==false) { 
				$this->error(L('_ERROR_'));
			}			
		}else{ 
			$this->error(L('_ERROR_'));
		} 
    }    
    
}